package com.kma.summer2012.client.view.mainPage.widgets;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.cellview.client.Column;
import com.google.gwt.user.cellview.client.ColumnSortEvent;
import com.google.gwt.user.cellview.client.DataGrid;
import com.google.gwt.user.client.ui.*;
import com.google.gwt.view.client.ListDataProvider;
import com.google.gwt.view.client.SelectionModel;
import com.kma.summer2012.client.presenters.mainPage.widgets.MainTablePresenter;
import com.kma.summer2012.client.styles.style2.Recources;
import com.kma.summer2012.client.view.mainPage.ColumnsFactory;
import com.kma.summer2012.shared.model.UICrumbsRequest;
import com.kma.summer2012.shared.model.UICrumbsUser;

import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * User: EUGENE
 * Date: 3/2/13
 * Time: 12:38 PM
 * To change this template use File | Settings | File Templates.
 */
public class MainTable extends Composite implements MainTablePresenter.Display {
    @UiField
    protected Button updateButton;
    @UiField
    protected ResizeLayoutPanel placeForDataGrid;
    @UiField
    protected HorizontalPanel additionalControlsContainer;

    protected DataGrid<UICrumbsRequest> cellTable;
    ListDataProvider<UICrumbsRequest> model;
    private final Integer tableID = (new Random()).nextInt(1000) + 1;
    private Column dateColumn = null;
    private Column idColumn = null;

    private String position;

    public void setPosition(String position){
        this.position = position;
    }

    public Integer getTableID() {
        return this.tableID;
    }


    interface MainTableUiBinder extends UiBinder<HTMLPanel, MainTable> {
    }

    private static MainTableUiBinder ourUiBinder = GWT.create(MainTableUiBinder.class);

    public MainTable() {
        HTMLPanel rootElement = ourUiBinder.createAndBindUi(this);
        updateButton.addStyleName(Recources.INSTANCE.styles().Caption());
        this.initWidget(rootElement);
        if (this.cellTable == null) {
            this.cellTable = new DataGrid<UICrumbsRequest>();
            this.placeForDataGrid.add(this.cellTable);
        }
        this.cellTable.setPageSize(9999);
    }

    public HasClickHandlers getUpdateButton() {
        return updateButton;
    }

    @Override
    public void cleanCellTable() {
        if (this.model != null)
            this.model.refresh();
        this.cellTable.redraw();
    }

    private int columnsCount = 0;

    public void configureDataGrid(List<Column> columns, ColumnSortEvent.ListHandler<UICrumbsRequest> listHandler) {
        this.cellTable.addColumnSortHandler(listHandler);
        while (this.columnsCount > 0) {
            this.cellTable.removeColumn(0);
            --this.columnsCount;
        }
        this.dateColumn = null;

        if (columns == null) {
            return;
        } else {
            for (Column column : columns) {
                String name = ((ColumnsFactory.HasName) column).getName();
                cellTable.addColumn(column, name);
                Integer width = ColumnsFactory.getInstance().getColumnWidth(name);
                cellTable.setColumnWidth(column, String.valueOf(width));
                if ((this.position != null) && this.position.equals(UICrumbsUser.MANAGER_POSITION)){
                    if (((ColumnsFactory.HasName) column).getName().equals(ColumnsFactory.REQUESTED_TIME_DATE)) {
                        this.dateColumn = column;
                    }
                }
                if ((this.position != null) && this.position.equals(UICrumbsUser.ENGINEER_POSITION)){
                    if (((ColumnsFactory.HasName) column).getName().equals(ColumnsFactory.ASSIGNED_DATE_TIME)) {
                        this.dateColumn = column;
                    }
                }
                if (((ColumnsFactory.HasName) column).getName().equals(ColumnsFactory.ID)) {
                    this.idColumn = column;
                }
                ++columnsCount;
            }
        }

        this.cellTable.getColumnSortList().push(this.idColumn);
        ColumnSortEvent.fire(cellTable, cellTable.getColumnSortList());
    }

    public ListDataProvider<UICrumbsRequest> fillDataGrid(List<UICrumbsRequest> uiCrumbsRequests) {
        this.model = new ListDataProvider<UICrumbsRequest>(uiCrumbsRequests);
        this.model.addDataDisplay(this.cellTable);
        return this.model;
    }

    public void setSelectionModelForTable(SelectionModel<UICrumbsRequest> selectionModel) {
        cellTable.setSelectionModel(selectionModel);
    }

    @Override
    public void setDoubleClickHandler(DoubleClickHandler doubleClickHandler) {
        this.cellTable.addDomHandler(doubleClickHandler, DoubleClickEvent.getType());
    }


    @Override
    public void sortDateColumn(String date) {
        if (this.dateColumn != null) {
            if ((this.position != null) && (this.position.equals(UICrumbsUser.MANAGER_POSITION))){
                ((ColumnsFactory.RequestedTimeColumn) this.dateColumn).dateToSortWith = date;
                this.cellTable.getColumnSortList().clear();
                this.cellTable.getColumnSortList().push(this.cellTable.getColumn(0));
                this.cellTable.getColumnSortList().push(this.dateColumn);
                ColumnSortEvent.fire(cellTable, cellTable.getColumnSortList());
                ((ColumnsFactory.RequestedTimeColumn) this.dateColumn).dateToSortWith = "";
            }
            if ((this.position != null) && (this.position.equals(UICrumbsUser.ENGINEER_POSITION))){
                ((ColumnsFactory.AssignedDateTimeColumn) this.dateColumn).dateToSortWith = date;
                this.cellTable.getColumnSortList().clear();
                this.cellTable.getColumnSortList().push(this.cellTable.getColumn(0));
                this.cellTable.getColumnSortList().push(this.dateColumn);
                ColumnSortEvent.fire(cellTable, cellTable.getColumnSortList());
                ((ColumnsFactory.AssignedDateTimeColumn) this.dateColumn).dateToSortWith = "";
            }
        }
    }

    public void setAdditionalControls(Widget widget) {
        this.additionalControlsContainer.add(widget);
    }
}